package com.csot.queue;

import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class QueueKit {

	private volatile boolean Flag = true; // 默认开启，进行生产+消费
	private BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();

	// 生产者
	public void produce(Runnable r) {

		if (Flag) {
			blockingQueue.offer(r);
		}

	}

	// 消费者
	public void consumer() throws InterruptedException {
		while (Flag) {
			System.out.println("消费对象");
			Runnable r = blockingQueue.take();
			if (Objects.nonNull(r))
				r.run();
		}
	}

	// 停止
	public void stop() {
		this.Flag = false;
	}

	public void start() throws InterruptedException {
		this.consumer();
	}

}
